package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private TbBusinessTrackRecordMapper businessTrackRecordMapper;


    @Autowired
    private SysDictDataMapper dictDataMapper;




    /**
     * 添加商机跟进记录
     * @param businessTrackVo
     */
    @Override
    @Transactional
    public void addBusinessTrackRecord(BusinessTrackVo businessTrackVo) {

        //创建商机对象
        TbBusiness business = new TbBusiness();

        BeanUtils.copyBeanProp(business,businessTrackVo);
        //封装商机id
        business.setId(businessTrackVo.getBusinessId());
        //封装最后修改时间
        business.setUpdateTime(DateUtils.getDateHm());
        //封装修改人
        business.setUpdateBy(SecurityUtils.getUsername());
        business.setStatus(TbClue.StatusType.FOLLOWING.getValue());

        //创建商机跟进记录对象
        TbBusinessTrackRecord businessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyBeanProp(businessTrackRecord,businessTrackVo);

        //封装跟进人
        businessTrackRecord.setCreateBy(SecurityUtils.getUsername());

        //封装跟进时间
        businessTrackRecord.setCreateTime(DateUtils.getDateHm());

        //更新商机表
        businessMapper.updateTbBusiness(business);

        //添加商机跟进表
        businessTrackRecordMapper.addBusinessTrackRecord(businessTrackRecord);

    }

    /**
     * 查询商机跟进数据
     * @param id
     * @return
     */
    @Override
    public List<TbBusinessTrackRecord> list(Long id) {
        //查询跟进数据数据
        List<TbBusinessTrackRecord> businessTrackRecordList = businessTrackRecordMapper.selectBusinessTrackRecord(id);

        //遍历查询字典
        for (TbBusinessTrackRecord businessTrackRecord : businessTrackRecordList) {
            List<String> keys = businessTrackRecord.getKeys();

            //获取沟通重点
            String[] KeyItems = businessTrackRecord.getKeyItems().split(",");

            //遍历查询字典
            for (String keyItem : KeyItems) {
                String label = dictDataMapper.selectDictLabel("communication_point", keyItem);
                keys.add(label);
            }
            businessTrackRecord.setKeys(keys);
        }

        return businessTrackRecordList;
    }
}
